<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<script src="/webjars/jquery/3.2.0/jquery.js"></script>
<style>
    input {
        width: 200px;
        height: 20px;
        margin: 10px;
    }

    /*    表单居中*/
    table {
        margin: 0 auto;
        width: 300px;
    }

    .tab-login {
        /*居中*/
        margin: 0 auto;
        width: 300px;
        text-align: center;
    }
</style>
<body>
<p class="tab-login" style="width: 300px;height: 200px">
    <input type="text" name="uName" placeholder="请输入用户名"><br>
    <input type="password" name="uPwd" placeholder="请输入密码"><br>
    <input type="submit" id="submit" value="登录"><br>
</p>

</body>
</html>

<script th:inline="javascript">

    var userId = [[${userId}]];

    $(function () {
        $("#submit").click(function () {
            var uName = $("input[name=uName]").val();
            var uPwd = $("input[name=uPwd]").val();
            if (uName == "" || uPwd == "") {
                alert("用户名或密码不能为空");
                return false;
            } else {
                $.post(
                    "http://localhost:8080/login",
                    {
                        uName: uName,
                        uPwd: uPwd
                    },
                    function (data) {
                        // alert(data)
                        console.log(data);
                        console.log(data.message)
                        console.log(data.userId)
                        console.log(data.userType)
                        if (data.message =="登录成功") {
                            sessionStorage.clear();
                            sessionStorage.setItem("uType", data.userType);
                            sessionStorage.setItem("uId", data.userId)
                            // 跳转页面index
                            window.location.href = "http://localhost:8080/selectAll";
                        } else {
                            alert("登录失败")
                        }
                    }
                )
            }
        })
    })
</script>